package graphics;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LinearGradientPaint;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class HSVColorTrack extends JPanel {
	private final Color H0 = new Color(255, 0, 0);
	private final Color H1 = new Color(255, 255, 0);
	private final Color H2 = new Color(0, 255, 0);
	private final Color H3 = new Color(0, 255, 255);
	private final Color H4 = new Color(0, 0, 255);
	private final Color H5 = new Color(255, 0, 255);

	private final Color[] BASE = { H0, H1, H2, H3, H4, H5, H0 };

	public HSVColorTrack() {
		super();
	}

	@Override
	public void paintComponent(Graphics og) {
		super.paintComponent(og);
		Graphics2D g = (Graphics2D) og;

		Insets insets = getInsets();
		Dimension size = getSize();
		final double width = size.getWidth() - insets.left - insets.right, height = size.getHeight() - insets.top - insets.bottom;
		Point2D start = new Point2D.Double(0, 0), end = new Point2D.Double(0, height);
		float[] pts = { 0f, 1f / 6, 2f / 6, 3f / 6, 4f / 6, 5f / 6, 1f };
		LinearGradientPaint linGrad = new LinearGradientPaint(start, end, pts, BASE);

		g.setPaint(linGrad);
		g.fill(new Rectangle2D.Double(0, 0, width, height));
	}

	public static void main(String... args) {
		JFrame f = new JFrame("Linear HSV Color Track");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		f.getContentPane().add(new HSVColorTrack(), BorderLayout.CENTER);
		f.setSize(20, 250);
		f.setVisible(true);
	}
}